
/* **************************************************** */
/*!
   @file	MN_DMD_Tuner_template.c
   @brief	Tuner API wrapper (sample)
   @author	R.Mori
   @date	2011/6/30
   @param
		(c)	Panasonic
   */
/* **************************************************** */
//---------- this is sample Tuner API  ----------------
#include "MN_DMD_driver.h"
#include "MN_DMD_common.h"
#if (TUNER_TYPE == MXL603)   
//#include "../../combo/tun_mxl603/MxL603_TunerApi.h"
//#include "../../combo/tun_mxl603/MaxLinearDataTypes.h"
#elif (TUNER_TYPE == NXP18250B)
// NXP Architecture includes
#include "tmCompId.h"
#include "tmNxtypes.h"
#include "tmFrontend.h"
#include "tmbslFrontEndCfgItem.h"
#include "tmbslFrontEndTypes.h"

// Components includes
#include "tmbslTDA18250A.h"
#elif (TUNER_TYPE == NXP18275)
// NXP Architecture includes
#include "tmNxTypes.h"
#include "tmCompId.h"
#include "tmFrontEnd.h"
#include "tmbslFrontEndTypes.h"
#include "tmUnitParams.h"
// Components includes
#include "tmbslTDA18275.h"
#elif (TUNER_TYPE == MXL301)
#include "mxl_common.h"
#include "mxl_api.h"
extern MxLxxxRF_TunerConfigS Tuner_set_para_global;
#else
#endif
/* **************************************************** */

//#define MN_TUNER_DEBUG
#ifdef MN_TUNER_DEBUG
#define MN_TUNER_PRINTF		libc_printf
#else
#define MN_TUNER_PRINTF(...)	do{}while(0)
#endif

/* **************************************************** */

/*! Tuner Initialize Function*/
DMD_ERROR_t DMD_API DMD_Tuner_init( )
{
	//TODO:	Please call tuner initialize API here

	DMD_ERROR_t ret = DMD_E_ERROR ;
#if (TUNER_TYPE == MXL603)   
	//ret = MXL603_INIT( );//jary20140331
	ret = DMD_E_OK;
#elif (TUNER_TYPE == NXP18250B)
	ret = tmbsliTDA18250A_Init( );	
#elif (TUNER_TYPE == NXP18275)
	ret = tmbsliTDA18275_Init(0);
#else	
#endif
     if (ret != DMD_E_OK)
    {
      MN_TUNER_PRINTF("Error! DMD_Tuner_init\n");    
     }
 
	//this function is called by DMD_init
	
	//DMD_DBG_TRACE("Tuner is not implemeted\n");
	return ret ;
}

/*! Tuner Tune Function */
DMD_ERROR_t DMD_API DMD_Tuner_tune( DMD_PARAMETER_t *param)
{
	//TODO:	Please call tune  API here
	DMD_ERROR_t ret = DMD_E_OK;
	DMD_u32_t rf_in_hz = param->freq; 
#if (TUNER_TYPE == MXL603)
	//jary20140331
	//ret = Mxl603SetFreqBw( ); //global parameter elsewhere 
#elif (TUNER_TYPE == NXP18250B)
	ret = tmbslTDA18250A_SetRF(rf_in_hz);
#elif (TUNER_TYPE == NXP18275)
	tmbslFrontEndState_t PLLLock = tmbslFrontEndStateUnknown;
	ret = tmbslTDA18275_SetRF(0, rf_in_hz);
	if(ret == DMD_E_OK)
	{
		/* Get TDA18275 PLL Lock status */
		ret = tmbslTDA18275_GetLockStatus(0, &PLLLock);
		if(ret == DMD_E_OK)
		{
			if (PLLLock != tmbslFrontEndStateLocked)
			{
			ret = DMD_E_ERROR; //signal not locked.
			}
		}
	}
#else
#endif
	if (ret != DMD_E_OK)
	{
		MN_TUNER_PRINTF(" Pls. retune - until tuner output stable .\n");    
		ret = DMD_E_ERROR;
	}
	 
	//this function is called by DMD_tune
	//DMD_DBG_TRACE("Tuner is not implemeted\n");
	return ret;
}

/*! Tuner Termination Function */
DMD_ERROR_t DMD_API DMD_Tuner_term( )
{
	//TODO:	Please call tune  API here
	//this function is called by DMD_term
	DMD_DBG_TRACE("Tuner is not implemeted\n");
	return DMD_E_OK;
}

/*! Tuner Tune Function */
DMD_ERROR_t DMD_API DMD_Tuner_set_system( DMD_PARAMETER_t *param)
{
	//TODO:	Please call tune  API here
		DMD_ERROR_t ret = DMD_E_ERROR;
#if (TUNER_TYPE == MXL603)   
		//jary20140331
		//ret = Mxl603SetSystemMode( );//global parameter elsewhere
		ret = DMD_E_OK;
#elif (TUNER_TYPE == NXP18250B)
		DMD_BANDWIDTH_t dmd_bw = param->bw;
		TDA18250AStandardMode_t tuner_bw;
		if (( param->system == DMD_E_DVBT2)||(param->system == DMD_E_DVBT))
		{		
			switch(dmd_bw)
			{
				case DMD_E_BW_6MHZ:           
					tuner_bw = TDA18250A_DVBT_6MHz;
					break;
				case DMD_E_BW_7MHZ:          
					tuner_bw = TDA18250A_DVBT_7MHz;
					break;      
				case DMD_E_BW_8MHZ:            
					tuner_bw = TDA18250A_DVBT_8MHz;
					break;
				case DMD_E_BW_5MHZ:
				case DMD_E_BW_1_7MHZ:
				default:  
					MN_TUNER_PRINTF("--- UNSUPPORT BANDWIDTH!!---\n");
				return DMD_E_ERROR;
			}
		}
		else if( param->system == DMD_E_DVBC)
		{
			tuner_bw = TDA18250A_QAM_8MHz;
		}
		else
		{
			MN_TUNER_PRINTF("--- UNSUPPORT SYSTEM!!---\n");
			return DMD_E_ERROR;
		}
		ret = tmbslTDA18250A_SetStandardMode(  tuner_bw);
#elif (TUNER_TYPE == NXP18275)
	DMD_BANDWIDTH_t dmd_bw = param->bw;
	tmTDA18275StandardMode_t tuner_bw;

    if (( param->system == DMD_E_DVBT2)||(param->system == DMD_E_DVBT))
   {		
        switch(dmd_bw)
		{
		case DMD_E_BW_6MHZ:           
				tuner_bw = tmTDA18275_DVBT_6MHz;
		        break;
		case DMD_E_BW_7MHZ:          
				tuner_bw = tmTDA18275_DVBT_7MHz;
		        break;      
		case DMD_E_BW_8MHZ:            
				tuner_bw = tmTDA18275_DVBT_8MHz;
		        break;
		case DMD_E_BW_5MHZ:
		case DMD_E_BW_1_7MHZ:
		default:  
       MN_TUNER_PRINTF("--- UNSUPPORT BANDWIDTH!!---\n");
				  return DMD_E_ERROR;
		}
    }
	else if( param->system == DMD_E_DVBC)
		{
		    tuner_bw = tmTDA18275_QAM_8MHz;
		}
	else
		{
			MN_TUNER_PRINTF("--- UNSUPPORT SYSTEM!!---\n");
			return DMD_E_ERROR;
		}
	 ret = tmbslTDA18275_SetStandardMode(0, tuner_bw);
#else
#endif
		if (ret != DMD_E_OK)
		{
		    MN_TUNER_PRINTF("Error! DMD_Tuner_set_system \n");    
		}	

		//this function is called by DMD_tune
		//DMD_DBG_TRACE("Tuner is not implemeted\n");
		return ret ;
}

#if 0
void Tuner_check_RF_input_power(DMD_u8_t tuner_id, int* rxPwrPtr)
{
#if (TUNER_TYPE == MXL603)  
	DMD_u8_t devId;
	devId = tuner_id;
	MxLWare603_API_ReqTunerRxPower(devId, rxPwrPtr); 
#elif (TUNER_TYPE == NXP18250B)
	tmbslTDA18250A_GetPowerLevel(rxPwrPtr);
#elif (TUNER_TYPE == NXP18275)
	tmbslTDA18275_GetPowerLevel(0, rxPwrPtr); //dBuV
#else
#endif 
}
#endif

